package com.ysk.testapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;

import com.ysk.testapplication.qcm.FillMoreMethod;
import com.ysk.testapplication.qcm.OnViewCreate;
import com.ysk.testapplication.qcm.ViewController;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

/**
 * @author RQ
 * date    2022-01-27 15:37
 */
public class BaseActivity extends AppCompatActivity {
  ViewController mViewController;

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mViewController = new ViewController(this);
    if (this instanceof OnViewCreate) {
      setContentView(((OnViewCreate) this).layoutId());
      mViewController.onViewCreate();
    }
  }

  public void setData2View(final int viewId, final Object obj) {
    mViewController.setData2View(viewId, obj);
  }

  //  FragmentManager mFragmentManager;
  //  @Override
  //  public boolean fillCustomViewData(View view, Object obj) {
  //    if (view instanceof FrameLayout) {
  //
  //      if (mFragmentManager == null) {
  //        mFragmentManager = getSupportFragmentManager();
  //      }
  //      FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction();
  //      Fragment fragment = null;
  //      //      if (obj instanceof Class) {
  //      //        fragment = CommonFragment.instance((Class<BaseController>) obj);
  //      //      } else
  //      if (obj instanceof BaseFragment) {
  //        fragment = (BaseFragment) obj;
  //      }
  //      if (fragment == null) {
  //        LOG.d("BaseController", "fillFrameLayout.fragment == null");
  //        return false;
  //      }
  //      FragmentTransaction ft = mFragmentTransaction;
  //      Fragment showFg = null;
  //      String tagPage = null;
  //      Fragment show = null;
  //      for (Fragment item : mFragmentManager.getFragments()) {
  //        if (!item.isHidden()) {
  //          show = item;
  //        }
  //      }
  //      try {
  //        tagPage = (String) view.getTag(R.id.fragment_tag_key);
  //        showFg = mFragmentManager.findFragmentByTag(tagPage);
  //      } catch (Exception e) {
  //      }
  //      if (!fragment.isAdded() && null == mFragmentManager.findFragmentByTag(tagPage)) {
  //        if (showFg != null) {
  //          ft.hide(showFg).add(view.getId(), fragment, tagPage);
  //        } else {
  //          view.setTag(R.id.fragment_tag_key, tagPage);
  //          ft.add(view.getId(), fragment, tagPage);
  //        }
  //      } else { //已经加载进容器里去了....
  //        if (show != null) {
  //          ft.hide(show).show(fragment);
  //        } else {
  //          ft.show(fragment);
  //        }
  //      }
  //      if (!isFinishing()) {
  //        ft.commitAllowingStateLoss();
  //        getSupportFragmentManager().executePendingTransactions();
  //      }
  //      return true;
  //    }
  //    return false;
  //  }
}
